package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import grails.converters.JSON
import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class LayoutController 
{
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Layout.list(params), model:[layoutCount: Layout.count()]
    }

    def show(Layout layout) {
        respond layout
    }

    def create() {
        respond new Layout(params)
    }

    @Transactional
    def save(Layout layout) {
        if (layout == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (layout.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond layout.errors, view:'create'
            return
        }

        layout.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'layout.label', default: 'Layout'), layout.id])
                redirect layout
            }
            '*' { respond layout, [status: CREATED] }
        }
    }

    def edit(Layout layout) {
        respond layout
    }

    @Transactional
    def update(Layout layout) {
        if (layout == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (layout.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond layout.errors, view:'edit'
            return
        }

        layout.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'layout.label', default: 'Layout'), layout.id])
                redirect layout
            }
            '*'{ respond layout, [status: OK] }
        }
    }

    @Transactional
    def delete(Layout layout) {

        if (layout == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        layout.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'layout.label', default: 'Layout'), layout.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'layout.label', default: 'Layout'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
